/**
 * Oct 6, 2012 7:08:48 AM
 * com.kratonsolution.cbs.common
 * Numbers.java
 */
package com.kratonsolution.belian.common.util;


/**
 * @author Agung Dodi Perdana
 * @email agung.dodi.perdana@gmail.com
 * @Version Sinch 0.0.1
 */
public class Numbers
{
    public static int getIntOrZero(Object object)
    {
        Integer out = Numbers.checkInt(object);
        return out != null? out : 0;
    }
    
    public static int getIntOrOne(Object object)
    {
        Integer out = Numbers.checkInt(object);
        return out != null? out : 1;
    }
    
    public static int getInt(Object object,int ifNull)
    {
        Integer out = Numbers.checkInt(object);
        return out != null? out:ifNull;
    }
    
    private static Integer checkInt(Object object)
    {
        if(object != null)
        {
            if(object instanceof Integer)
                return (Integer)object;
            else if(object instanceof Long)
                return ((Long)object).intValue();
            else if(object instanceof Double)
                return ((Double)object).intValue();
        }
        
        return null;
    }
    
    public static final synchronized long toLong(Object object)
    {
        if(object != null && object instanceof Long)
            return (java.lang.Long)object;
            
        return 0L;
    }
}
